package com.ai.service.made.impl;

import com.ai.common.CommonResponse;
import com.ai.entity.made.LiveBO;
import com.ai.networking.websocket.service.WebSocket;
import com.ai.networking.websocket.service.comm.CommonWebSocket;
import com.ai.networking.websocket.service.entity.WSMessage;
import com.ai.service.made.LiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LiveServiceImpl implements LiveService {

    /**
     * 直播
     *
     * @param liveBo
     * @return
     */
    @Override
    public CommonResponse live(LiveBO liveBo) {

        if (CommonWebSocket.WS_LIVE_MAP.containsKey(liveBo.getCameraId())
                && CommonWebSocket.WS_LIVE_MAP.get(liveBo.getCameraId()) != null
                && CommonWebSocket.WS_LIVE_MAP.get(liveBo.getCameraId()).size() > 0) {
            WSMessage wsMessage = new WSMessage()
                    .setType("live")
                    .setCode(liveBo.getCameraId())
                    .setContent(liveBo.getSnapPhoto());

            //获取所有正在开启直播的摄像头id的列表
            for (WebSocket webSocket : CommonWebSocket.WS_LIVE_MAP.get(liveBo.getCameraId())) {
                webSocket.sendMessage(wsMessage);
            }
        }
        return CommonResponse.ok("直播发送成功");
    }
}
